package com.kamloo.stg.fragment;

import java.util.ArrayList;

import com.kamloo.stg.adapter.TabBusStopArrayAdapter;
import com.kamloo.stg.data.db.Node;
import com.kamloo.stg.data.db.NodeDAO;
import com.kamloo.stg.data.db.Stop;
import com.kamloo.stg.data.db.StopDAO;
import com.kamloo.stg.dialog.TabBusPeriodDialog;
import com.kamloo.stg.info.AppConstant;
import com.kamloo.stg.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;

public class TabBusStop extends Fragment {
	// ----- GLOBAL VARIABLES -----
	private int m_directionId = -1;
	private ListView m_busStopList;
	ArrayList<String> m_values;
	ArrayList<Stop> m_stops;
	ArrayList<Node> m_nodes;
	
	//----- LISTENER -----
	OnItemClickListener busStopClickListener = new OnItemClickListener()
    {
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    	{
    		FragmentManager manager = getActivity().getSupportFragmentManager();
    		TabBusPeriodDialog dialog = new TabBusPeriodDialog();
   		    
    		int nodeId = Integer.parseInt((String) ((TextView)view.findViewById(R.id.tabBusStopRowNodeId)).getText());
    		    		    		    		
    		dialog.initializeView(nodeId);    		
    		dialog.show(manager, AppConstant.FRAGMENT_TAB_BUS_PERIOD_DIALOG);
    	}
    };	    

	// ----- CLASS CODE -----
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (container == null) {
			return null;
		}

		return (LinearLayout) inflater.inflate(R.layout.tab_bus_stop, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		m_busStopList = (ListView) view.findViewById(R.id.tabBusStopList);
		if (m_directionId != -1) {
			initializeData();
		}
	}	
	
	private void initializeData(){
		m_values = new ArrayList<String>();
		m_stops = new ArrayList<Stop>();
		m_nodes = new ArrayList<Node>();
		
		setBusStopData();
		TabBusStopArrayAdapter adapter = new TabBusStopArrayAdapter(getActivity(), m_values.toArray(new String[m_values.size()]), 
				                                                    m_stops, m_nodes);

		// Assign adapter and listener to ListView
		m_busStopList.setAdapter(adapter);
		m_busStopList.setOnItemClickListener(busStopClickListener);
	}

	public void initializeView(int directionId) {
		m_directionId = directionId;
		initializeData();
	}

	private void setBusStopData() {
		NodeDAO nodeDao = new NodeDAO(getActivity());
		StopDAO stopDao = new StopDAO(getActivity());
    	nodeDao.open();
    	stopDao.open();
    	
    	ArrayList<Node> nodes = nodeDao.selectFromDirection(m_directionId);
    	for (Node node : nodes) {
    		m_nodes.add(node);
    		
    		Stop stop = stopDao.select(node.getFk_stopId());
    		m_stops.add(stop);
    		m_values.add(stop.getName());
    	}
    	
    	nodeDao.close();
    	stopDao.close();
	}
}
